#include "tlrsyntaxhighlighter.h"
#include <QDebug>

TLRSyntaxHighlighter::TLRSyntaxHighlighter(QTextDocument *parent)
    : QSyntaxHighlighter(parent)
{

}

/*
 * 设置语言格式
 * nsyntaxfmt : 语言格式
 */
void TLRSyntaxHighlighter::setSyntaxFormat(const TSyntaxFormat &nsyntaxfmt)
{
    m_syntaxFmt = nsyntaxfmt;
    switch (m_syntaxFmt) {
    case TSyntaxFormat::syntaxFmtJson: {
        m_highlightRules.clear();
        THighlightRule nrule;
        //JSON 其它字符
        nrule.pattern = QRegularExpression("[!-~]");
        nrule.textFormat.setFontWeight(QFont::Bold);
        nrule.textFormat.setForeground(Qt::white);
        m_highlightRules.append(nrule);
        //JSON 中括号
        nrule.pattern = QRegularExpression("[\\[-\\]]");
        nrule.textFormat.setFontWeight(QFont::Bold);
        nrule.textFormat.setForeground(QColor(0x33CCDB));
        m_highlightRules.append(nrule);
        //JSON 大括号
        nrule.pattern = QRegularExpression("[\\{-\\}]");
        nrule.textFormat.setFontWeight(QFont::Bold);
        nrule.textFormat.setForeground(QColor(0xD4D4D4));
        m_highlightRules.append(nrule);
        //JSON 普通字母
        nrule.pattern = QRegularExpression("\\b[A-Za-z]+\\b");
        nrule.textFormat.setFontWeight(QFont::Bold);
        nrule.textFormat.setForeground(Qt::red);
        m_highlightRules.append(nrule);
        //JSON 普通数字
        nrule.pattern = QRegularExpression("\\b[0-9]+\\b");
        nrule.textFormat.setFontWeight(QFont::Bold);
        nrule.textFormat.setForeground(QColor(0xB5CEA8));
        m_highlightRules.append(nrule);
        //JSON 逻辑值
        nrule.pattern = QRegularExpression("\\bfalse\\b");
        nrule.textFormat.setFontWeight(QFont::Bold);
        nrule.textFormat.setForeground(QColor(0xB5CEA8));
        m_highlightRules.append(nrule);
        nrule.pattern = QRegularExpression("\\btrue\\b");
        nrule.textFormat.setFontWeight(QFont::Bold);
        nrule.textFormat.setForeground(QColor(0xB5CEA8));
        m_highlightRules.append(nrule);
        //JSON Value String格式
        nrule.pattern = QRegularExpression("\".*\"");
        nrule.textFormat.setFontWeight(QFont::Bold);
        nrule.textFormat.setForeground(QColor(0xCE9178));
        m_highlightRules.append(nrule);
        //JSON Key格式
        nrule.pattern = QRegularExpression("\".*\":");
        nrule.textFormat.setFontWeight(QFont::Bold);
        nrule.textFormat.setForeground(QColor(0x9CDCFE));
        m_highlightRules.append(nrule);
        break;
    }
    case TSyntaxFormat::syntaxFmtCpp: {
        m_highlightRules.clear();
        THighlightRule nrule;
        //JSON 其它字符
        nrule.pattern = QRegularExpression("[!-~]");
        nrule.textFormat.setFontWeight(QFont::Bold);
        nrule.textFormat.setForeground(Qt::white);
        m_highlightRules.append(nrule);
        //关键字
        QStringList nkeywordlist = {"\\bstruct\\b","\\bchar\\b","\\bu8\\b","\\bu16\\b","\\bs16\\b","\\bu32\\b","\\bs32\\b"};
        for(int ikeyword=0;ikeyword<nkeywordlist.size();ikeyword++) {
            nrule.pattern = QRegularExpression(nkeywordlist.at(ikeyword));
            nrule.textFormat.setFontWeight(QFont::Bold);
            nrule.textFormat.setForeground(QColor(0x569cD7));
            m_highlightRules.append(nrule);
        }
        //JSON 中括号
        nrule.pattern = QRegularExpression("[\\[-\\]]");
        nrule.textFormat.setFontWeight(QFont::Bold);
        nrule.textFormat.setForeground(QColor(0x33CCDB));
        m_highlightRules.append(nrule);
        //JSON 大括号
        nrule.pattern = QRegularExpression("[\\{-\\}]");
        nrule.textFormat.setFontWeight(QFont::Bold);
        nrule.textFormat.setForeground(QColor(0xD4D4D4));
        m_highlightRules.append(nrule);
        //JSON 普通数字
        nrule.pattern = QRegularExpression("\\b[0-9]+\\b");
        nrule.textFormat.setFontWeight(QFont::Bold);
        nrule.textFormat.setForeground(QColor(0xB5CEA8));
        m_highlightRules.append(nrule);
        break;
    }
    }
}

void TLRSyntaxHighlighter::highlightBlock(const QString &text)
{
    foreach (const THighlightRule &nrule, m_highlightRules) {
        QRegularExpressionMatchIterator matchIterator = nrule.pattern.globalMatch(text);
        while (matchIterator.hasNext()) {
            QRegularExpressionMatch match = matchIterator.next();
            setFormat(match.capturedStart(), match.capturedLength(), nrule.textFormat);
        }
    }
}
